using AjeeleeCms.Core.Common;
using AjeeleeCms.Core.DBUtility;
using AjeeleeCms.Web.Plugin.Feedback.Model;
using System;
using System.Data;
using System.Data.OleDb;
using System.Text;
namespace AjeeleeCms.Web.Plugin.Feedback.DAL
{
	public class feedback
	{
		public bool Exists(int id)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("select count(1) from dt_feedback");
			stringBuilder.Append(" where id=@id");
			OleDbParameter[] array = new OleDbParameter[]
			{
				new OleDbParameter("@id", OleDbType.Integer, 4)
			};
			array[0].Value = id;
			return DbHelperOleDb.Exists(stringBuilder.ToString(), array);
		}
		public int Add(AjeeleeCms.Web.Plugin.Feedback.Model.feedback model)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("insert into dt_feedback(");
			stringBuilder.Append("title,content,user_name,user_tel,user_qq,user_email,add_time,is_lock)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@title,@content,@user_name,@user_tel,@user_qq,@user_email,@add_time,@is_lock)");
			OleDbParameter[] array = new OleDbParameter[]
			{
				new OleDbParameter("@title", OleDbType.VarChar, 100), 
				new OleDbParameter("@content", OleDbType.VarChar), 
				new OleDbParameter("@user_name", OleDbType.VarChar, 50), 
				new OleDbParameter("@user_tel", OleDbType.VarChar, 30), 
				new OleDbParameter("@user_qq", OleDbType.VarChar, 30), 
				new OleDbParameter("@user_email", OleDbType.VarChar, 100), 
				new OleDbParameter("@add_time", OleDbType.Date), 
				new OleDbParameter("@is_lock", OleDbType.Integer, 4)
			};
			array[0].Value = model.title;
			array[1].Value = model.content;
			array[2].Value = model.user_name;
			array[3].Value = model.user_tel;
			array[4].Value = model.user_qq;
			array[5].Value = model.user_email;
			array[6].Value = model.add_time;
			array[7].Value = model.is_lock;
			return DbHelperOleDb.ExecuteSql(stringBuilder.ToString(), array);
		}
		public void UpdateField(int id, string strValue)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("update dt_feedback set " + strValue);
			stringBuilder.Append(" where id=" + id);
			DbHelperOleDb.ExecuteSql(stringBuilder.ToString());
		}
		public bool Update(AjeeleeCms.Web.Plugin.Feedback.Model.feedback model)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("update dt_feedback set ");
			stringBuilder.Append("title=@title,");
			stringBuilder.Append("content=@content,");
			stringBuilder.Append("user_name=@user_name,");
			stringBuilder.Append("user_tel=@user_tel,");
			stringBuilder.Append("user_qq=@user_qq,");
			stringBuilder.Append("user_email=@user_email,");
			stringBuilder.Append("add_time=@add_time,");
			stringBuilder.Append("reply_content=@reply_content,");
			stringBuilder.Append("reply_time=@reply_time,");
			stringBuilder.Append("is_lock=@is_lock");
			stringBuilder.Append(" where id=@id");
			OleDbParameter[] array = new OleDbParameter[]
			{
				new OleDbParameter("@title", OleDbType.VarChar, 100), 
				new OleDbParameter("@content", OleDbType.VarChar), 
				new OleDbParameter("@user_name", OleDbType.VarChar, 50), 
				new OleDbParameter("@user_tel", OleDbType.VarChar, 30), 
				new OleDbParameter("@user_qq", OleDbType.VarChar, 30), 
				new OleDbParameter("@user_email", OleDbType.VarChar, 100), 
				new OleDbParameter("@add_time", OleDbType.Date), 
				new OleDbParameter("@reply_content", OleDbType.VarChar), 
				new OleDbParameter("@reply_time", OleDbType.Date), 
				new OleDbParameter("@is_lock", OleDbType.Integer, 4), 
				new OleDbParameter("@id", OleDbType.Integer, 4)
			};
			array[0].Value = model.title;
			array[1].Value = model.content;
			array[2].Value = model.user_name;
			array[3].Value = model.user_tel;
			array[4].Value = model.user_qq;
			array[5].Value = model.user_email;
			array[6].Value = model.add_time;
			array[7].Value = model.reply_content;
			array[8].Value = model.reply_time;
			array[9].Value = model.is_lock;
			array[10].Value = model.id;
			int num = DbHelperOleDb.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(int id)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("delete from dt_feedback ");
			stringBuilder.Append(" where id=@id");
			OleDbParameter[] array = new OleDbParameter[]
			{
				new OleDbParameter("@id", OleDbType.Integer, 4)
			};
			array[0].Value = id;
			int num = DbHelperOleDb.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public AjeeleeCms.Web.Plugin.Feedback.Model.feedback GetModel(int id)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("select  top 1 id,title,content,user_name,user_tel,user_qq,user_email,add_time,reply_content,reply_time,is_lock from dt_feedback ");
			stringBuilder.Append(" where id=@id");
			OleDbParameter[] array = new OleDbParameter[]
			{
				new OleDbParameter("@id", OleDbType.Integer, 4)
			};
			array[0].Value = id;
			AjeeleeCms.Web.Plugin.Feedback.Model.feedback feedback = new AjeeleeCms.Web.Plugin.Feedback.Model.feedback();
			DataSet dataSet = DbHelperOleDb.Query(stringBuilder.ToString(), array);
			AjeeleeCms.Web.Plugin.Feedback.Model.feedback result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				if (dataSet.Tables[0].Rows[0]["id"] != null && dataSet.Tables[0].Rows[0]["id"].ToString() != "")
				{
					feedback.id = int.Parse(dataSet.Tables[0].Rows[0]["id"].ToString());
				}
				if (dataSet.Tables[0].Rows[0]["title"] != null && dataSet.Tables[0].Rows[0]["title"].ToString() != "")
				{
					feedback.title = dataSet.Tables[0].Rows[0]["title"].ToString();
				}
				if (dataSet.Tables[0].Rows[0]["content"] != null && dataSet.Tables[0].Rows[0]["content"].ToString() != "")
				{
					feedback.content = dataSet.Tables[0].Rows[0]["content"].ToString();
				}
				if (dataSet.Tables[0].Rows[0]["user_name"] != null && dataSet.Tables[0].Rows[0]["user_name"].ToString() != "")
				{
					feedback.user_name = dataSet.Tables[0].Rows[0]["user_name"].ToString();
				}
				if (dataSet.Tables[0].Rows[0]["user_tel"] != null && dataSet.Tables[0].Rows[0]["user_tel"].ToString() != "")
				{
					feedback.user_tel = dataSet.Tables[0].Rows[0]["user_tel"].ToString();
				}
				if (dataSet.Tables[0].Rows[0]["user_qq"] != null && dataSet.Tables[0].Rows[0]["user_qq"].ToString() != "")
				{
					feedback.user_qq = dataSet.Tables[0].Rows[0]["user_qq"].ToString();
				}
				if (dataSet.Tables[0].Rows[0]["user_email"] != null && dataSet.Tables[0].Rows[0]["user_email"].ToString() != "")
				{
					feedback.user_email = dataSet.Tables[0].Rows[0]["user_email"].ToString();
				}
				if (dataSet.Tables[0].Rows[0]["add_time"] != null && dataSet.Tables[0].Rows[0]["add_time"].ToString() != "")
				{
					feedback.add_time = DateTime.Parse(dataSet.Tables[0].Rows[0]["add_time"].ToString());
				}
				if (dataSet.Tables[0].Rows[0]["reply_content"] != null && dataSet.Tables[0].Rows[0]["reply_content"].ToString() != "")
				{
					feedback.reply_content = dataSet.Tables[0].Rows[0]["reply_content"].ToString();
				}
				if (dataSet.Tables[0].Rows[0]["reply_time"] != null && dataSet.Tables[0].Rows[0]["reply_time"].ToString() != "")
				{
					feedback.reply_time = new DateTime?(DateTime.Parse(dataSet.Tables[0].Rows[0]["reply_time"].ToString()));
				}
				if (dataSet.Tables[0].Rows[0]["is_lock"] != null && dataSet.Tables[0].Rows[0]["is_lock"].ToString() != "")
				{
					feedback.is_lock = int.Parse(dataSet.Tables[0].Rows[0]["is_lock"].ToString());
				}
				result = feedback;
			}
			else
			{
				result = null;
			}
			return result;
		}
		public DataSet GetList(int Top, string strWhere)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" id,title,content,user_name,user_tel,user_qq,user_email,add_time,reply_content,reply_time,is_lock ");
			stringBuilder.Append(" FROM dt_feedback ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by add_time desc");
			return DbHelperOleDb.Query(stringBuilder.ToString());
		}
		public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("select * FROM dt_feedback");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			recordCount = Convert.ToInt32(DbHelperOleDb.GetSingle(PagingHelper.CreateCountingSql(stringBuilder.ToString())));
			return DbHelperOleDb.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, stringBuilder.ToString(), filedOrder));
		}
	}
}
